package com.aix.demo.activity;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridLayout;

import com.aix.demo.R;

public class GridLayoutActivity extends Activity {
	
	private GridLayout gridLayout;
	
	private Button mAddBtn;
	
	private OnClickListener onRemoveBtnClickListener;
	
	private int quarterScreenWidth;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_gridlayout);
		gridLayout = (GridLayout) findViewById(R.id.gridlayout);
		
		onRemoveBtnClickListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				gridLayout.removeView(v);
			}
		};
		
		Point size = new Point();
		getWindowManager().getDefaultDisplay().getSize(size);
		int screenWidth = size.x;
		int screenHeight = size.y;
		int halfScreenWidth = (int) (screenWidth * 0.5);
		quarterScreenWidth = (int) (halfScreenWidth * 0.5);
		
		for (int i = 0; i < 6; ++i) {
			addBtn(0);
		}
		
//		ViewGroup.LayoutParams layoutParams = gridLayout.getLayoutParams();
//		layoutParams.height = 4 * quarterScreenWidth;
//		gridLayout.setLayoutParams(layoutParams);
		
		mAddBtn = (Button) findViewById(R.id.add_btn);
		mAddBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				addBtn(0);
			}
		});
	}
	
	private void addBtn(int index) {
		Button btn = new Button(this);
		btn.setOnClickListener(onRemoveBtnClickListener);
		GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
//		layoutParams.columnSpec = GridLayout.spec(i % 4);
//		layoutParams.rowSpec = GridLayout.spec(i / 4);
		layoutParams.width = quarterScreenWidth;
		layoutParams.height = quarterScreenWidth;
		btn.setLayoutParams(layoutParams);
		btn.setText("button");
		gridLayout.addView(btn, index);
	}
}
